<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/

/**
 * Start the tag cloud
 * @author John Cox
 * @author Roger Raymond
 * @author Carl Corliss (help)
 * @author Jo dalle Nogare 
 * @access  public
 * @return  array
*/

function conversations_user_main()
{
    // If we mark all as read
    if (!xarVarFetch('read', 'isset', $read, NULL, XARVAR_DONT_SET)) return;
    if (!xarVarFetch('subs', 'isset', $subs, NULL, XARVAR_DONT_SET)) return;
    if (!xarVarFetch('hot', 'isset', $subs, NULL, XARVAR_DONT_SET)) return;
    if (!xarVarFetch('q', 'str:1:', $q, '', XARVAR_NOT_REQUIRED)) return;

    // Security Check
    if (!xarSecurityCheck('ViewCon')) return;
    
    // We need to grab our array early since we are going to be doing this on a one by one basis
    $consettings = xarModGetVar('conversations', 'settings');
    if (!empty($consettings)) {
        $settings = unserialize($consettings);
    }

    // Variable Needed for output
    $data               = array();
    $data['uid']        = xarUserGetVar('uid');
    $data['now']        = time();
    $data['timestamp']  = $settings['initial'];
    $data['items']      = array();

    // OK, lets get the cloud
    if (isset($subs)){
        // Just the subscriptions, ma'am
        $tags = xarModAPIFunc('conversations', 'user', 'gettags_byuid', array(
                              'uid' => $data['uid']//,
                              //'status'    => 1 // hmmm.
                              ));
    } elseif (!empty($q)){
        $tags = xarModAPIFunc('conversations', 'user', 'gettagid_bypost_search', array(
                              'q' => $q//,
                              //'status'    => 1 // hmmm.
                              ));
    } else {        
        // Normal View
        $tags = xarModAPIFunc('conversations', 'user', 'gettags', array(
                              'timestamp' => $data['timestamp']//,
                              //'status'    => 1 // hmmm.
                               ));
    }
    // return var_export($tags);
    
    $totaltags = count($tags);
    // Loop for the selected tags
    for ($i = 0; $i < $totaltags; $i++) {
        $tag = $tags[$i];
        
        // Mark as read.  This is a little painful.  Probably going to need to purge at some point.
        // Have we been marked as read?        
        if ((isset($read)) or (!xarUserIsLoggedIn())){
            if (xarUserIsLoggedIn()){
                xarModAPIFunc('conversations', 'user', 'updatevisits', array(
                                  'uid' => $data['uid'],
                                  'tagid' => $tag['id']));
                $tags[$i]['new_post'] = false;
            } else {
                $tags[$i]['new_post'] = false;
            }   
        } else {
            // If the last visit time is earlier than the timestamp, then there is a new post.
            $cookie_monster =     xarModAPIFunc('conversations', 'user', 'getvisits', array(
                                                                        'tagid'   => $tag['id'],
                                                                        'uid'   => xarUserGetVar('uid')));
                                                                        
                                                                      
            // The first thing we know is if visits is empty, then the user has never been there.
            if (empty($cookie_monster)){
                $tags[$i]['new_post'] = true;
                $tags[$i]['from'] = $tags[$i]['lastvisit'];
            } else {
                // The next thing we know is, when the lastvisit is inserted into the tag, 
                // we should be able to compare
                foreach ($cookie_monster as $visit){
                  
                        //return var_export($tags[$i]['lastvisit']);
                  
                    if ($visit['timestamp'] > $tags[$i]['lastvisit']){
                      
                        // If our visit timestamp is greater than our tag timestamp, then no new post                    
                        $tags[$i]['new_post'] = false;
                    } else {
                        // Otherwise, there is a new post, methinks  
                        // Lets also send the timestamp for a direct link to the latest post
                        $tags[$i]['from'] = $visit['timestamp'];
                        $tags[$i]['new_post'] = true;
                    }
                }
            }
        }



        
        // Check for subscriptions
        
        $tags[$i]['check_subs'] = xarModAPIFunc('conversations', 'user', 'getsubs', array(                                                                                        
                                                'tagid'   => $tag['id'],
                                                'uid'     => xarUserGetVar('uid')));
        
        
        
        
        // Obviously not perfect, and will need some work, but I can't think of a better way right now
        // Maybe a feature request will give me some ideas.
        
        // OK, Now lets figure out the class of the cloud
        // Options for the tag cloud
        // What we want to do is a sliding scale.  What importance do we place and how does that rank
        // Then we just have to switch through our scale and adjust the css
        // Here are our factors
        //
        // Last post = fresh
        // Count = how highly rated / visited
        // Status of post = is it locked?  Is it sticky?
        // Number of posts in tag

        // Now we need to get this on a scale between one and ten for ease
        // A fresh post should add a point.  if there is a post in the past x hours, fresh. -- admin option
        $cloud = 0;
        
        if ($tags[$i]['lastvisit'] > (time() - 60*60*2)){ // xarModGetVar('conversations', 'fresh');
            $cloud = $cloud + 2;
        }
        
        // Finally, our rating and our visits.
        // $good = unserialized(xarModGetVar('conversations', 'good'));
        if (($tag['count'] >= 100) and ($tag['count'] <= 200)){ 
            $cloud += 1;

        } elseif (($tag['count'] >= 201) and ($tag['count'] <= 300)){
            $cloud += 2;
            
        } elseif (($tag['count'] >= 301) and ($tag['count'] <= 400)){
            $cloud += 3;
        
        } elseif (($tag['count'] >= 401) and ($tag['count'] <= 500)){
            $cloud += 4;
            
        } elseif (($tag['count'] >= 501) and ($tag['count'] <= 600)){
            $cloud += 5;

        } elseif (($tag['count'] >= 601) and ($tag['count'] <= 700)){
            $cloud += 6;
            
        } elseif (($tag['count'] >= 701) and ($tag['count'] <= 800)){
            $cloud += 7;
        
        } elseif (($tag['count'] >= 801) and ($tag['count'] <= 900)){
            $cloud += 8;
            
        } elseif (($tag['count'] >= 901) and ($tag['count'] <= 1000)){
            $cloud += 9;
            
        } elseif ($tag['count'] >= 1001){
            $cloud += 10;
        }
        
        if ($tags[$i]['lastvisit'] < (time() - 60*60*48)){ // xarModGetVar('conversations', 'fresh');
            $cloud = $cloud - 2;
        }
        
        // Cap it at 10
        if ($cloud > 10){
          $cloud = 10;
        }
        
        // TODO: Think about penalties to points.
        // So we start with the first case.  New Post, Hot Topic, Popular Topic
        switch(strtolower($cloud)) {
          case 0:
          default:
            $tags[$i]['cloudclass'] = 'cloud-zero';
            break;
          case 1:
            $tags[$i]['cloudclass'] = 'cloud-one';
            break;
          case 2: 
            $tags[$i]['cloudclass'] = 'cloud-two';
            break;
          case 3:
            $tags[$i]['cloudclass'] = 'cloud-three';
            break;
          case 4:
            $tags[$i]['cloudclass'] = 'cloud-four';
            break;
          case 5:
            $tags[$i]['cloudclass'] = 'cloud-five';
            break;
          case 6:
            $tags[$i]['cloudclass'] = 'cloud-six';
            break;
          case 7:
            $tags[$i]['cloudclass'] = 'cloud-seven';
            break;
          case 8:
            $tags[$i]['cloudclass'] = 'cloud-eight';
            break;
          case 9:
            $tags[$i]['cloudclass'] = 'cloud-nine'; //heh...
            break;
          case 10:
            $tags[$i]['cloudclass'] = 'cloud-ten';
            break;
        }
    }
    
    // Add the array of items to the template variables
    $data['items'] = $tags;

    xarTplSetPageTitle(xarML('Conversations'));
    return $data;
}
?>